package baseFx.tests.common.mq.rabbitmq;

import baseFx.common.AutoCloseable;
import baseFx.common.Var;
import baseFx.common.concurrent.Task;
import baseFx.common.lock.AppLockspace;
import baseFx.common.lock.Lockspace;
import baseFx.common.lock.LockspaceException;
import baseFx.common.mq.rabbitmq.RabbitMqContext;
import baseFx.common.mq.rabbitmq.RabbitMqPublisher;
import baseFx.common.mq.rabbitmq.RabbitMqSubscriber;
import com.rabbitmq.client.BuiltinExchangeType;

import java.util.Arrays;
import java.util.Scanner;
import java.util.UUID;

public class Run {
    public static void main(String[] args) {
        RabbitMqContext context = new RabbitMqContext("127.0.0.1", 5672, "guest", "guest");
        String ex = "testMq_1347099c-c423-492c-b686-4d8b722266a9";
        String queue = "testQueue_1347099c-c423-492c-b686-4d8b722266a9";
        String q2 = "testQueue_4548131_4878dwad_hkahwjhkda464";

        if (context.existsExchange(ex) == false) {
            context.exchangeDeclare(ex, BuiltinExchangeType.FANOUT);
        }
        Lockspace lockspace = new AppLockspace();
        RabbitMqSubscriber subscriber = new RabbitMqSubscriber(context);
        subscriber.eventReceived().add((s, e) -> {
            System.out.println("data=" + e.data);
        });
        subscriber.eventError().add((s, e) -> {
            System.out.println("error=" + e.error.getMessage());
            e.isRetry = false;
        });
        subscriber.consume(ex, queue);

        RabbitMqPublisher publisher = new RabbitMqPublisher(context);
        Scanner scanner = new Scanner(System.in);
        while ("0".equals(scanner.nextLine()) == false) {
            String str = UUID.randomUUID().toString();
            publisher.post(ex, str);
            System.out.println("completed");
        }
        System.exit(0);
    }

}
